﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using FineUICore;
using Microsoft.AspNetCore.Mvc;
using MyAppBox.Controllers;
using Newtonsoft.Json.Linq;
using SysFrame.BP;
using SysFrame.Message;

namespace MyAppBox.Areas.system.Controllers
{
    [Area("system")]
    public class system2Controller : SysPageController
    {
        // GET: system/system2
        public ActionResult Index()
        {
            InitPage();
            return View();
        }

        private void InitPage()
        {
            MsQuery msq = new MsQuery(" and 1=1");
            DataTable dt = ((DataSet)(new BLLsystem_2().Getsystem_2ByQuery(msq).data)).Tables[0];
            IList<FineUICore.TreeNode> listtn = FineUIEx.OnInitTreeDataBind(dt, "SYSTEM_2_30", "SYSTEM_2_10", "SYSTEM_2_70");
            ViewBag.TreeMenuNodes = listtn.ToArray();
            dt = FineUIEx.GetNewTreeDt(dt, "SYSTEM_2_10", "SYSTEM_2_70");
            ViewBag.ddlsys2_70DataSource = dt;
            if (UIHelper.IsFineUIAjaxPostBack())
            {
                var Tree1 = UIHelper.Tree("Tree1");
                Tree1.LoadData(listtn);
            }
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult EditFormsave(JObject ms)
        {
            Mssystem_2 ms2 = Mssystem_2.parse(ms);
            MsReturned rt = new BLLsystem_2().Savesystem_2(ms2);
            if (Convert.ToInt32(rt.strMsId) >= 0)
            {
                InitPage();
                var ddlsys2_70 = UIHelper.DropDownList("ddlsys2_70");
                relodeddl70(rt.strMsId);
                ddlsys2_70.SelectedValue(ms2.System_2_70);
            }
            return FineUICore.UIHelper.Result();
        }

        public void relodeddl70(string rowid)
        {
            MsQuery msq = new MsQuery(" and 1=1");
            msq.strquery += " and SYSTEM_2_10!='" + rowid + "'";
            DataTable dt = ((DataSet)(new BLLsystem_2().Getsystem_2ByQuery(msq).data)).Tables[0];
            dt = FineUIEx.GetNewTreeDt(dt, "SYSTEM_2_10", "SYSTEM_2_70");
            var ddlsys2_70 = UIHelper.DropDownList("ddlsys2_70");
            ddlsys2_70.DataSource(dt, "SYSTEM_2_10", "SYSTEM_2_30", "_TreeLevel");
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Tree1_NodeClick(string nodeId, JArray fields)
        {
            MsQuery msq = new MsQuery();
            msq.strquery += " and system_2.SYSTEM_2_10='" + nodeId + "' or system_2.SYSTEM_2_70='" + nodeId + "'";
            //DataTable dt = ((DataSet)(new BLLsystem_2().Getsystem_2ByQuery(msq).data)).Tables[0];
            List<Mssystem_2> listms = new BLLsystem_2().Getsystem_2MsByQuery(msq);
            var Grid1 = UIHelper.Grid("sys2Grid");
            Grid1.DataSource(listms, fields);
            return FineUICore.UIHelper.Result();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult sys2Grid_RowClick(string rowid, JArray fields)
        {
            MsQuery msq = new MsQuery();
            msq.strquery += " and system_2.SYSTEM_2_10='" + rowid + "'";
            Mssystem_2 ms = new BLLsystem_2().Getsystem_2MsByKey(rowid);
            var editform = UIHelper.Form("EditForm");
            relodeddl70(rowid);
            editform.SetData(ms);
            return FineUICore.UIHelper.Result();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult relodeddlsys2_70(string rowid)
        {
            MsQuery msq = new MsQuery(" and 1=1 and (SYSTEM_2_70!='" + rowid + "' or SYSTEM_2_70 is null) and SYSTEM_2_10 !='" + rowid + "'");
            DataTable dt = ((DataSet)(new BLLsystem_2().Getsystem_2ByQuery(msq).data)).Tables[0];
            dt = FineUIEx.GetNewTreeDt(dt, "SYSTEM_2_10", "SYSTEM_2_70");
            var ddlsys2_70 = UIHelper.DropDownList("ddlsys2_70");
            ddlsys2_70.DataSource(dt, "SYSTEM_2_10", "SYSTEM_2_30", "_TreeLevel");
            msq = new MsQuery();
            msq.strquery += " and system_2.SYSTEM_2_10='" + rowid + "'";
            Mssystem_2 ms = new BLLsystem_2().Getsystem_2MsByKey(rowid);
            ddlsys2_70.SelectedValue(ms.System_2_70);
            return FineUICore.UIHelper.Result();
        }
    }
}